//
//  YCPopupDismissAnimation.m
//  YCAlertController
//
//  Created by Yochia on 2017/3/9.
//  Copyright © 2017年 Team Brutal. All rights reserved.
//

#import "YCPopupDismissAnimation.h"
#import "YCPopupController.h"
#import "YCPopupView.h"

@implementation YCPopupDismissAnimation

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    YCPopupController * fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    fromVC.view.alpha = 1;
    if ([fromVC.popupView respondsToSelector:@selector(popupViewDisappearAnimationOrigin)]) {
        [fromVC.popupView popupViewDisappearAnimationOrigin];
    }
    
    NSTimeInterval duration = [self transitionDuration:transitionContext];
    [UIView animateWithDuration:duration
                     animations:^{
                         fromVC.view.alpha = 0;
                         if ([fromVC.popupView respondsToSelector:@selector(popupViewDisappearAnimationDestination)]) {
                             [fromVC.popupView popupViewDisappearAnimationDestination];
                         }
                     }
                     completion:^(BOOL finished) {
                         [transitionContext completeTransition:YES];
                     }];
}

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.15;
}

@end
